package cn.admobiletop.adsuyi.adapter.gdt.listener;

import com.qq.e.ads.nativ.NativeExpressAD;
import com.qq.e.ads.nativ.NativeExpressADView;
import com.qq.e.comm.util.AdError;

import java.util.ArrayList;
import java.util.List;

import cn.admobiletop.adsuyi.ad.data.ADSuyiNativeAdInfo;
import cn.admobiletop.adsuyi.ad.error.ADSuyiError;
import cn.admobiletop.adsuyi.ad.listener.ADSuyiNativeAdListener;
import cn.admobiletop.adsuyi.adapter.gdt.data.NativeExpressAdInfo;
import cn.admobiletop.adsuyi.config.ADSuyiErrorConfig;
import cn.admobiletop.adsuyi.util.ADSuyiAdUtil;

/**
 * @author ciba
 * @description 模板信息流广告监听
 *              需要继承 BaseAdListener<ADSuyiNativeAdListener>，实现所适配渠道的信息流模版广告监听回调，并在回调中调用事先set的adListener，进行相应回调
 *              需要在渠道回调中调用ADSuyi以下方法：
 *                  getAdListener().onAdReceive(this)     广告获取
 *                  getAdListener().onAdExpose(this)      广告曝光
 *                  getAdListener().onAdClose(this)       广告关闭
 *                  getAdListener().onAdClick(this)       广告点击
 *                  getAdListener().onRenderFailed(this, new ADSuyiError(code, message))       广告渲染失败
 *                  onAdFailed(code, message)             广告失败
 * @date 2020/4/8
 */
public class NativeExpressAdListener extends BaseAdListener<ADSuyiNativeAdListener> implements NativeExpressAD.NativeExpressADListener {
    private List<ADSuyiNativeAdInfo> nativeAdInfoList;

    public NativeExpressAdListener(String platformPosId, ADSuyiNativeAdListener adListener) {
        super(platformPosId, adListener);
    }

    @Override
    public void onADLoaded(List<NativeExpressADView> nativeExpressADViewList) {
        if (nativeExpressADViewList == null || nativeExpressADViewList.isEmpty()) {
            onAdFailed(ADSuyiErrorConfig.AD_FAILED_AD_IS_EMPTY, ADSuyiErrorConfig.MSG_AD_FAILED_AD_IS_EMPTY);
            return;
        }

        if (getAdListener() != null) {
            nativeAdInfoList = new ArrayList<>();
            for (int i = 0; i < nativeExpressADViewList.size(); i++) {
                NativeExpressAdInfo nativeExpressAdInfo = new NativeExpressAdInfo(getPlatformPosId());
                nativeExpressAdInfo.setAdapterAdInfo(nativeExpressADViewList.get(i));
                nativeExpressAdInfo.setAdListener(getAdListener());
                nativeAdInfoList.add(nativeExpressAdInfo);
            }
            getAdListener().onAdReceive(nativeAdInfoList);
        }
    }

    @Override
    public void onRenderFail(NativeExpressADView nativeExpressADView) {
        if (getAdListener() != null) {
            ADSuyiNativeAdInfo expressAdInfo = ADSuyiAdUtil.getAdInfoWithAdapterAdInfo(nativeAdInfoList, nativeExpressADView);
            if (expressAdInfo != null) {
                getAdListener().onRenderFailed(expressAdInfo, new ADSuyiError(-1, "unknown"));
            }
        }
    }

    @Override
    public void onRenderSuccess(NativeExpressADView nativeExpressADView) {}

    @Override
    public void onADExposure(NativeExpressADView nativeExpressADView) {
        if (getAdListener() != null) {
            ADSuyiNativeAdInfo expressAdInfo = ADSuyiAdUtil.getAdInfoWithAdapterAdInfo(nativeAdInfoList, nativeExpressADView);
            if (expressAdInfo != null) {
                getAdListener().onAdExpose(expressAdInfo);
            }
        }
    }

    @Override
    public void onADClicked(NativeExpressADView nativeExpressADView) {
        if (getAdListener() != null) {
            ADSuyiNativeAdInfo expressAdInfo = ADSuyiAdUtil.getAdInfoWithAdapterAdInfo(nativeAdInfoList, nativeExpressADView);
            if (expressAdInfo != null) {
                getAdListener().onAdClick(expressAdInfo);
            }
        }
    }

    @Override
    public void onADClosed(NativeExpressADView nativeExpressADView) {
        if (getAdListener() != null) {
            ADSuyiNativeAdInfo expressAdInfo = ADSuyiAdUtil.getAdInfoWithAdapterAdInfo(nativeAdInfoList, nativeExpressADView);
            if (expressAdInfo != null) {
                getAdListener().onAdClose(expressAdInfo);
            }
        }
    }

    @Override
    public void onADLeftApplication(NativeExpressADView nativeExpressADView) {}

    @Override
    public void onADOpenOverlay(NativeExpressADView nativeExpressADView) {}

    @Override
    public void onADCloseOverlay(NativeExpressADView nativeExpressADView) {}

    @Override
    public void onNoAD(AdError adError) {
        onAdFailed(adError.getErrorCode(), adError.getErrorMsg());
    }

    @Override
    public void release() {
        super.release();
        ADSuyiAdUtil.releaseList(nativeAdInfoList);
        nativeAdInfoList = null;
    }
}
